package com.mutual.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.mutual.dao.FinanceDao;
import com.mutual.dao.IDividendDao;
import com.mutual.dao.IMemberDao;
import com.mutual.entity.Dividend;
import com.mutual.entity.Finance;
import com.mutual.entity.Member;

@Component("quartzBean")
public class Quartz{
	
	private Logger logger = Logger.getLogger(Quartz.class);
	@Resource
	private IDividendDao dividendDaoImpl;
	@Resource
	private IMemberDao memberDaoImpl;
	@Resource
	private FinanceDao financeDaoImpl;
	private BigDecimal sum = null;
	
	public void execute() {//定时任务触发的方法
		
		//计算收益
		rifenhong();
		
		//推荐奖收益
		tuijian();
		
		// 计算团队将
		teamBonus();
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes", "rawtypes" })
	public void rifenhong(){
		//计算收益  持有股*10
		//1.查出所有会员持有股
		List<Member> ListMem = memberDaoImpl.listMemberAllUse();
		for (int i = 0; i < ListMem.size(); i++) {
			String id= ListMem.get(i).getId();
			Map map = new HashMap();
			map.put("id",id);//会员id
			map.put("status",0);//日分红
			Dividend dividend = dividendDaoImpl.getDividendByMemberId(map);
			if(null != dividend){
				int chiyougu = dividend.getStockNumber();//持有股
				Dividend div = new Dividend();
				//收益余额大于等于150出局一股
				if(dividend.getOutNumber().compareTo(new BigDecimal(150)) >= 0){
					//更新出局股数-1；
					div.setOutStock(dividend.getOutStock()-1);
					//收益余额-150；
					div.setOutNumber(dividend.getOutNumber().subtract(new BigDecimal(150)));
				}
				//2向日分红表插入数据
				div.setDayDividend(new BigDecimal(chiyougu).multiply(new BigDecimal(10)));
				div.setOutNumber(dividend.getOutNumber().add(new BigDecimal(chiyougu*10)));
				div.setTotalNumber(dividend.getTotalNumber().add(new BigDecimal(chiyougu*10)));
				div.setMemberId(id);
				div.setStatus(0);
				div.setIncomeTime(new Date());
				div.setOutStock(dividend.getOutStock());
				div.setTotalStock(dividend.getTotalStock());
				div.setStockNumber(chiyougu);
				dividendDaoImpl.add(div);
				//3更新余额
				Finance fin = financeDaoImpl.selectByPrimaryKey(id);
				Finance fin2 = new Finance();
				fin2.setMemberId(fin.getMemberId());
				fin2.setNumber(fin.getNumber().add(new BigDecimal(chiyougu*10D)));
				financeDaoImpl.updateByPrimaryKeySelective(fin2);
			}
		}
	}
	//推荐奖
	@SuppressWarnings("unchecked")
	public void tuijian(){
		List<Member> ListMem = memberDaoImpl.listMemberAll();
		for (int i = 0; i < ListMem.size(); i++) {
			String code = ListMem.get(i).getName();
			//查出此会员 推荐的会员
			List<Member> Lm = memberDaoImpl.getMemberByInvokeCode("code");
			BigDecimal num = new BigDecimal(0);
			for (int j = 0; j < Lm.size(); j++) {
				Map map = new HashMap();
				map.put("id",Lm.get(i).getId());//会员id
				map.put("status",0);//日分红
				Dividend dividend = dividendDaoImpl.getDividendByMemberId(map);
				num = num.add(dividend.getDayDividend().multiply(new BigDecimal(0.1)));
			}
			//更新推荐奖
			Finance fin = financeDaoImpl.selectByPrimaryKey(ListMem.get(i).getId());
			if(null != fin){
				Finance fin2 = new Finance();
				fin2.setMemberId(fin.getMemberId());
				fin2.setGroomAward(fin.getGroomAward().add(num));
				financeDaoImpl.updateByPrimaryKeySelective(fin2);
			}
		}
		
	}
	
	public void setDividendDaoImpl(IDividendDao dividendDaoImpl) {
		this.dividendDaoImpl = dividendDaoImpl;
	}
	public IDividendDao getDividendDaoImpl() {
		return dividendDaoImpl;
	}
	public void setMemberDaoImpl(IMemberDao memberDaoImpl) {
		this.memberDaoImpl = memberDaoImpl;
	}
	public IMemberDao getMemberDaoImpl() {
		return memberDaoImpl;
	}
	
	public void teamBonus(){
		List<Member> memberList = memberDaoImpl.listMemberAll();
		for (Member item : memberList) {
			logger.debug("会员的信息开始："+item.getName());
			sum = new BigDecimal(0);
			calcProfit(item.getName(),new BigDecimal(1),1);
			Dividend dividend = new Dividend();
			dividend.setTotalNumber(sum);
			dividend.setMemberId(item.getId());
			dividend.setStatus(4);
			dividendDaoImpl.addTeam(dividend);
			Finance finance = financeDaoImpl.selectByPrimaryKey(item.getId());
			if(null != finance){
				finance.setMemberId(item.getId());
				finance.setTeamAward(sum);
				finance.setNumber(finance.getNumber().add(sum));
				finance.setGroomAward(new BigDecimal(0));
				financeDaoImpl.updateByPrimaryKeySelective(finance);
			}
			logger.debug("会员："+item.getName()+"的团队奖是："+ sum.setScale(2,BigDecimal.ROUND_HALF_UP));
		}
		
	}
	
	@SuppressWarnings("unchecked")
	public void calcProfit(String invokeCode,BigDecimal percent,Integer level){
		Member member = new Member();
		member.setInvokeCode(invokeCode);
		logger.debug("'我'的直推会员：");
		List<Member> memList = memberDaoImpl.listByCondition(member);
		for (Member item : memList) {
			
			if(level == 10){
				break;
			}
			// 获取当前会员的持有股
			Map map = new HashMap();
			map.put("id",item.getId());//会员id
			map.put("status",0);//日分红
			Dividend dividend = dividendDaoImpl.getByMemberId(map);
			if(null != dividend){
				sum = sum.add(percent.multiply(new BigDecimal(dividend.getStockNumber() == 0 ? 1 : dividend.getStockNumber())));
			}
			calcProfit(item.getName(),percent.subtract(new BigDecimal(0.1)),(level+1));
		}
	}
}
